<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ItemControler
 *
 * @author root
 */
include_once 'CommonController.php';
class ItemController extends CommonController{
    
    public function preDispatch() {
        
        $this->session = new Zend_Session_Namespace('default');
        if (!$this->session->items)
            $this->session->items = array();
        $this->currentItemId = count($this->session->items);
 
        
    }
    
    public function indexAction(){
        $this->view->items = $this->session->items;
    }
    
    public function createAction(){
        
        $this->view->form = new App_forms_ItemEditor($this->currentItemId, $this->session);
        
        if ($this->getRequest()->isPost()) {
            if($this->postBack())
                $this->_helper->Redirector->gotoUrl('/item/');

        }
    }
    
    
    public function editAction(){
        $this->view->itemId = $this->getRequest()->getParam('id');
        $this->view->form = new App_forms_ItemEditor($this->getRequest()->getParam('id'), $this->session, 'edit');
        if ($this->getRequest()->isPost()) {
            if($this->postBack())
                $this->_helper->Redirector->gotoUrl('/item/');

        }
    }
    
    
    /**
     * @return bool
     */
    private function postBack(){
            if ($this->view->form->isValid($this->getRequest()->getParams())){
                $this->session->items[$this->view->form->getValue('id')] = $this->view->form->MapFormToItemDto();
                return TRUE;
            }  else {
                $this->view->errorElements = $this->view->form->getMessages();
                
            }        
            return FALSE;
    }
}

?>
